<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>注册</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f8f8f8;
        }
        .container {
            width: 400px;
            margin: 2em auto;
            padding: 2em;
            background-color: #fff;
            border-radius: 5px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }
        .form-group {
            margin-bottom: 1em;
        }
        .form-group label {
            display: block;
            margin-bottom: 0.5em;
        }
        .form-group input {
            width: 100%;
            padding: 0.5em;
            font-size: 16px;
            border: 1px solid #dcdcdc;
            border-radius: 5px;
        }
        .button {
            padding: 0.5em 1em;
            font-size: 16px;
            border: none;
            border-radius: 5px;
            background-color: #4caf50;
            color: white;
            cursor: pointer;
        }
        .button:hover {
            background-color: #45a049;
        }
    </style>
</head>
<body>
<div class="container">
    <h2>注册</h2>
    <form action="sys/user/register" th:action="@{/sys/user/register}" th:method="post">
        <div class="form-group">
            <label for="username">用户名</label>
            <input type="text" id="username" name="username" required />
        </div>
        <div class="form-group">
            <label for="password">密码</label>
            <input type="password" id="password" name="password" required />
        </div>
        <div class="form-group">
            <label for="email">邮箱</label>
            <input type="email" id="email" name="email" required />
        </div>
        <button type="submit" class="button">注册</button>
    </form>
</div>
<script>
    // 获取表单元素
    const loginForm = document.querySelector('form');
    const usernameInput = document.getElementById('username');
    const passwordInput = document.getElementById('password');
    const email = document.getElementById('email');

    // 监听表单提交事件
    loginForm.addEventListener('submit', async function(e) {
        e.preventDefault(); // 阻止表单默认提交行为

        // 获取用户输入的数据
        const registerData = {
            username: usernameInput.value,
            password: passwordInput.value,
            email: email.value
        };

        try {
            // 调用后端登录接口
            const response = await fetch('sys/user/register', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify(registerData)
            });

            // 检查响应状态
            if (response.status===0) {
                const result = await response.json();

                // 登录成功处理
                if (result.success) {
                    alert('登录成功！');
                    // 可以跳转到其他页面
                    // window.location.href = '/dashboard';
                } else {
                    alert('登录失败：' + result.message);
                }
            } else {
                alert('网络错误，请稍后重试');
            }
        } catch (error) {
            console.error('登录请求失败：', error);
            alert('登录失败，请检查网络连接');
        }
    });

    // 可选：添加输入验证
    function validateForm() {
        if (!usernameInput.value.trim()) {
            alert('请输入用户名');
            return false;
        }
        if (!passwordInput.value.trim()) {
            alert('请输入密码');
            return false;
        }
        return true;
    }

    // 在提交前验证表单
    loginForm.addEventListener('submit', function(e) {
        if (!validateForm()) {
            e.preventDefault();
        }
    });
</script>
</body>
</html>
